import datetime
import logging
import os
import random
import string
import time

from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

log = logging.getLogger('sd')


def load_pipeline():
    start_time = time.time()
    global inpainting
    model_path = os.getenv('MODELSCOPE_CACHE_LAMA', "damo/cv_fft_inpainting_lama")
    inpainting = pipeline(Tasks.image_inpainting, model=model_path)
    log.info("load model time: {:.6f} seconds".format(time.time() - start_time))
    return inpainting


# 生成随机文件名
def get_png_name(url):
    file_extension = url.split("?")[0].split(".")[-1]
    random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
    file_name = f"{random_string}.{file_extension}"
    return file_name


def get_file_prefix():
    # return f"d:/tmp/output/{get_date_prefix()}/"
    return f"/mnt/output/{get_date_prefix()}/lama/"


def check_and_create_dir(file_path):
    directory = os.path.dirname(file_path)
    if not os.path.exists(directory):
        os.makedirs(directory)


def get_oss_file_url(file_name):
    return f"{get_date_prefix()}/lama/{file_name}"


def get_date_prefix():
    today = datetime.date.today()
    t = today.strftime('%Y%m%d')
    return t
